//-----------------------------------------------------------------------
// <copyright file="Delete.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Deletes the specified Sql Login (calls sp_revokelogin).</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Sql.Access
{
    using System;
    using System.Xml;
    using System.Globalization;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;
    using SdcSql = Microsoft.Sdc.Tasks.Configuration.SqlServer;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Revokes login access to the specified SQL 
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Sql.Access.Revoke UserName="userName" ServerName="serverName" />]]></code>
    /// <para>where:</para>
    /// <para><i>userName (Required)</i></para>
    /// <para>The login name to grant access to.</para>
    /// <para><i>ConnectionString</i></para>
    /// <para>A valid connection string used to connect the SQL server to run the operation on.</para>
    /// <para><i>ServerName</i></para>
    /// <para>The server to revoke access to (used instead of <see cref="SqlBase.ConnectionString"/>)</para>
    /// <para><i>ConnectionTimeout</i></para>
    /// <para>Number of seconds to wait before failing a connection attempt. 
    /// Used if a <see cref="SqlBase.ConnectionString"/> is not specified.</para>
    /// <para><i>CommandTimeout</i></para>
    /// <para>Number of seconds to wait before failing the revoke request. </para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Sql.Access.Revoke
    ///             UserName="testUser"
    ///             ServerName="(local)" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class Revoke : SqlBase
    {
        private string serverName = String.Empty;
        private string userName = String.Empty;

        /// <summary>
        /// Initializes a new instance of the DeleteSqlLogin class.
        /// </summary>
        public Revoke()
        {
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Execute code

            if (SdcSql.Login.ExistsWithConnectionString(userName, this.ConnectionString))
            {
                Log.LogMessageFromResources("Sql.DeleteLogin", userName);
                SdcSql.Login.DeleteWithConnectionString(userName, this.ConnectionString);
            }
            else
            {
                throw new ApplicationException(String.Format(CultureInfo.InvariantCulture, "Unable to revoke access from '{0}' because the user does not exist", this.userName));
            }


            #endregion
        }


        /// <summary>
        /// Login account to revoke access to
        /// </summary>
        /// <value>A valid login account, in the form machine\username</value>
        [Required]
        public string UserName
        {
            get
            {
                return (userName == null ? String.Empty : userName);
            }
            set
            {
                userName = value;
            }
        }
    }
}